/**
 * 647. 回文子串
 */
public class Solution_647 {
    /**
     * 官方题解：中心扩展
     * <p>
     * 时间复杂度：O(n^2)
     * <p>
     * 空间复杂度：O(1)
     */
    public int countSubstrings(String s) {
        int len = s.length(), ans = 0;
        for (int i = 0; i < 2 * len - 1; i++) {
            int left = i / 2, right = i / 2 + i % 2;
            while (left >= 0 && right < len && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
                ans++;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution_647 solution = new Solution_647();
        int ans = solution.countSubstrings("aaa");
        System.out.println(ans);
    }
}
